package cn.mst.vrs.service.venue;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.mst.vrs.controller.admin.venue.vo.VenuePageReqVO;
import cn.mst.vrs.controller.admin.venue.vo.VenueSaveReqVO;
import cn.mst.vrs.controller.app.venue.vo.VenueSearchReqVO;
import cn.mst.vrs.controller.app.venue.vo.VenueSearchRespVO;
import cn.mst.vrs.convert.VenueConvert;
import cn.mst.vrs.dal.dataobject.venue.VenueDO;
import cn.mst.vrs.dal.dataobject.venuetype.VenueTypeDO;
import cn.mst.vrs.dal.mysql.venue.VenueMapper;
import cn.mst.vrs.dal.mysql.venuetype.VenueTypeMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.mst.vrs.enums.ErrorCodeConstants.VENUE_NOT_EXISTS;

/**
 * 体育场馆 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class VenueServiceImpl implements VenueService {

    @Resource
    private VenueMapper venueMapper;

    @Resource
    private VenueTypeMapper venueTypeMapper;

    @Override
    public Long createVenue(VenueSaveReqVO createReqVO) {
        // 插入
        VenueDO venue = BeanUtils.toBean(createReqVO, VenueDO.class);
        venueMapper.insert(venue);
        // 返回
        return venue.getId();
    }

    @Override
    public void updateVenue(VenueSaveReqVO updateReqVO) {
        // 校验存在
        validateVenueExists(updateReqVO.getId());
        // 更新
        VenueDO updateObj = BeanUtils.toBean(updateReqVO, VenueDO.class);
        venueMapper.updateById(updateObj);
    }

    @Override
    public void deleteVenue(Long id) {
        // 校验存在
        validateVenueExists(id);
        // 删除
        venueMapper.deleteById(id);
    }

    private void validateVenueExists(Long id) {
        if (venueMapper.selectById(id) == null) {
            throw exception(VENUE_NOT_EXISTS);
        }
    }

    @Override
    public VenueDO getVenue(Long id) {
        return venueMapper.selectById(id);
    }

    @Override
    public PageResult<VenueDO> getVenuePage(VenuePageReqVO pageReqVO) {
        return venueMapper.selectPage(pageReqVO);
    }

    @Override
    public cn.mst.vrs.common.entity.PageResult<VenueSearchRespVO> searchPageVenue(VenueSearchReqVO reqVO) {
        Page<VenueDO> venueDOPage = venueMapper.selectSearchPage(Page.of(reqVO.getCurrent(), reqVO.getSize()), reqVO.getKeyword());
        List<Long> typeIdList = venueDOPage.getRecords().stream().map(VenueDO::getTypeId).distinct().collect(Collectors.toList());
        Map<Long, String> typeMap = new HashMap<>();
        venueTypeMapper.selectBatchIds(typeIdList).forEach((venueTypeDO -> {
            typeMap.put(venueTypeDO.getId(), venueTypeDO.getName());
        }));
        List<VenueSearchRespVO> searchRespVOList = venueDOPage.getRecords().stream().map(venueDO -> VenueConvert.INSTANCE.toSearchRespVO(venueDO, typeMap.get(venueDO.getTypeId()))).toList();
        return cn.mst.vrs.common.entity.PageResult.of(venueDOPage.getCurrent(), venueDOPage.getTotal(), searchRespVOList);
    }

    @Override
    public VenueSearchRespVO getVenueDetail(Long id) {
        VenueDO venueDO = venueMapper.selectById(id);
        if (venueDO == null) {
            throw exception(VENUE_NOT_EXISTS);
        }
        VenueTypeDO venueTypeDO = venueTypeMapper.selectById(venueDO.getTypeId());
        return VenueConvert.INSTANCE.toSearchRespVO(venueDO, venueTypeDO.getName());
    }

}